//
//  SNBaseViewController.m
//  project
//
//  Created by 黄建华 on 2017/11/16.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNBaseViewController.h"
@interface SNBaseViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic, assign) BOOL isCanSideBack;

@end

@implementation SNBaseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BBIGLIGHTCOLOR;
    
    _stateView = [[UIView alloc] init];
    _stateView.backgroundColor = BBIGLIGHTCOLOR;
    if (KK_ShareData.ipx)
    {
        _stateView.frame = SNFRAME(0, 0, HHHWIDTH, 44);
    }
    else
    {
        _stateView.frame = SNFRAME(0, 0, HHHWIDTH, 20);
    }
    [self.view addSubview:_stateView];
    
    _sbTitleLable = [[UILabel alloc] init];
    _sbTitleLable.textAlignment = NSTextAlignmentCenter;
    _sbTitleLable.frame = SNFRAME(0, FitSimpleIpx(32, 52), HHHWIDTH, 20);
//    _sbTitleLable.text = @"导航";
    _sbTitleLable.textColor = SNCOLORBLACK;
    _sbTitleLable.font = SNFONT(17.0);
    [self.view addSubview:_sbTitleLable];
    
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftButton.frame = SNFRAME(5, FitSimpleIpx(0, 20), 44, 44);
    [_leftButton  setImageNormal:@"进入"];
    [_leftButton setTitleColor:SNCOLORBLACK forState:UIControlStateNormal];
    _leftButton.titleLabel.font = SNSFONT(15.0);
    _leftButton.center = CGPointMake(25, _sbTitleLable.center.y);
    [_leftButton  addTarget:self
                     action:@selector(functionSetLeft)
           forControlEvents:UIControlEventTouchUpInside];
    _leftButton.hidden = YES;
    [self.view addSubview:_leftButton];
    
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = SNFRAME(5, FitSimpleIpx(0, 20), 60, 44);
    [_rightButton setTitleColor:SNCOLORBLACK forState:UIControlStateNormal];
    _rightButton.titleLabel.font = SNSFONT(15.0);
    _rightButton.center = CGPointMake(HHHWIDTH - _rightButton.width /2, _sbTitleLable.center.y);
    [_rightButton  addTarget:self
                      action:@selector(functionSetRight)
            forControlEvents:UIControlEventTouchUpInside];
    _rightButton.hidden = YES;
    [self.view addSubview:_rightButton];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SNBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SNBebackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)SNBecomeActive
{
    
}

- (void)SNBebackground
{
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)functionSetLeft
{
    
}

- (void)functionSetRight
{
    
}

- (void)resetSideBack {
    self.isCanSideBack = YES;
    //开启ios右滑返回
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

-(void)forbiddenSideBack{
    self.isCanSideBack = NO;
    //关闭ios右滑返回
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate=self;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {
    return self.isCanSideBack;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CustomTabBar *tabBar = (CustomTabBar *)self.tabBarController;
    [tabBar showTheTabBarView];
    
    KK_BLEConnect.updateModelStatus = ^(KKBLEModel *model, KKBLEUpdateType type)
    {
        [self updateModel:model type:type];
    };
    
    KK_BLEConnect.bleConnectAcceptBlock = ^(id object, KKBLEAcceptType type) {
        [self updateView:type];
    };
    
     [self forbiddenSideBack];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self resetSideBack];
}

// 通知界面蓝牙状态
- (void)updateModel:(KKBLEModel *)model type:(KKBLEUpdateType)type
{
    
}

// 通知界面接收数据刷新相应界面的UI UpdateUiType
- (void)updateView:(KKBLEAcceptType)type
{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)otaUpdateInfoView
{
    return;
    if (!KK_BLEModel.isConnected ) {
        return;
    }
    if (!KK_BLEModel.bleMAC) {
        return;
    }
    
    NSString *time = [[NSDate date]dateToDayString];
    NSString *key = [NSString stringWithFormat:@"%@ mac:%@",time,KK_BLEModel.bleMAC];
    if ([key getBOOLValue]) {
        NSLog(@"🙂今天已经保存过了:%@",key);
        return;
    }
    
    NSString *message = SN_Text(@"1.修复了iPhone ios13系统更新带来的推送与电话功能bug.\n2.其它bug修改");
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:SN_Text(@"新的固件更新") message:message preferredStyle:UIAlertControllerStyleAlert];
    UIView *subView1 = alertController.view.subviews[0];
    UIView *subView2 = subView1.subviews[0];
    UIView *subView3 = subView2.subviews[0];
    UIView *subView4 = subView3.subviews[0];
    UIView *subView5 = subView4.subviews[0];
    
    UILabel *mes= subView5.subviews[1];
    mes.textAlignment = NSTextAlignmentCenter;
    mes.adjustsFontSizeToFitWidth = YES;
    
    UILabel * titleLab = subView5.subviews[2];
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.adjustsFontSizeToFitWidth = YES;
    
       UIAlertAction *loginout = [UIAlertAction actionWithTitle:SN_Text(@"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
           [self otaUpdateInfoViewok];
       }];
    
       UIAlertAction *cancel = [UIAlertAction actionWithTitle:SN_Text(@"下次提醒我") style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
           [self otaUpdateInfoViewcancel];
       }];
       [loginout setValue:UIColorHEX(0x181818) forKey:@"titleTextColor"];
       [cancel setValue:UIColorHEX(0x181818) forKey:@"titleTextColor"];
       [alertController addAction:loginout];
       [alertController addAction:cancel];
       
       [self presentViewController:alertController animated:YES completion:nil];
}

- (void)otaUpdateInfoViewok
{
    NSString *time = [[NSDate date]dateToDayString];
    NSString *key = [NSString stringWithFormat:@"%@ mac:%@",time,KK_BLEModel.bleMAC];
    [key setBOOLValue:YES];
    OTAUpdateFirmwareNordicVc           *vc       = [[OTAUpdateFirmwareNordicVc alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)otaUpdateInfoViewcancel
{
    NSString *time = [[NSDate date]dateToDayString];
    NSString *key = [NSString stringWithFormat:@"%@ mac:%@",time,KK_BLEModel.bleMAC];
    [key setBOOLValue:YES];
}

@end
